#include <bits/stdc++.h>
using namespace std;
#define yes cout<<"YES"<<endl
#define no cout<<"NO"<<endl
using ll = long long;
void solve()
{
    ll m, a, b, c;
    cin >> m >> a >> b >> c;
    ll ans = 0;
    ll m1 =m, m2 = m;
    if(m<=a)
    {
        m1 = 0;
        ans += m;
    }
    else {
        m1 -= a;
        ans += a;
    }

    if(m<=b)
    {
        m2 = 0;
        ans += m;
    }
    else {
        m2 -= b;
        ans += b;
    }

    ll now = m1 + m2;
    if(now>=c)
    {
        ans += c;
    }
    else
        ans += now;

    cout << ans << endl;
}
int main()
{
    int t = 1;
    cin >> t;
    while(t--)
    {
        solve();
    }
   return 0;
}
